<?php

if (!defined('BASEPATH'))
    exit("不允许直接访问本文件");

class Login extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library("session");
        $this->load->library('myauth');
    }

    function index() {//载入登录视图
        $data['uid'] = $this->session->userdata("uid");
        $data['uname'] = $this->session->userdata("uname");
        $data['is_admin'] =$this->myauth->check_permission('p6');
        if (empty($data['uid']))
            $this->load->view('login.php', $data);
        else {
            $ret = $this->db->where('uid',$data['uid'])->get('user')->result_array();
            $data['p_list'] = $ret[0];
            $this->load->view('login_out.php', $data);
        }
    }

    function log_in() {
        $uname = $_POST["uname"];
        $upass = $_POST["password"];
        $this->db->select('*');
        $this->db->from('user');
        $this->db->where('uname', $uname);

        $user = $this->db->get(); //查询用户名
        $user = $user->result();
        if ($user) {
            if ($user[0]->upass == $upass) {//密码正确
                //加载session类，注意使用session类需要修改config文件 
                $arr = array("uid" => $user[0]->uid, "uname" => $user[0]->uname);
                $this->session->set_userdata($arr);
                echo $this->session->userdata("uname");
            }
            else
                echo "password_error";
        }
        else {
            echo "user_invalid";
        }
    }

    function check_session() {//检测是否登录
        //header(http-equiv="Content-Type" content="text/html; charset=UTF-8");
        if ($this->session->userdata("uid")) {//session存在
            echo $this->session->userdata("uname") . "已登录";
        } else {
            echo "没登陆啊！ 魂淡";
        }
    }

    function logout() {
        $this->load->library('session');
        $this->session->unset_userdata("uid"); //关闭session
        $this->session->unset_userdata("uname");
        redirect('/login', 'refresh');
    }

}

